package com.whoiszxl.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.whoiszxl.command.MemberApplicationService;
import com.whoiszxl.core.entity.ResponseResult;
import com.whoiszxl.core.entity.response.PageResponse;
import com.whoiszxl.query.MemberQueryApplicationService;
import com.whoiszxl.query.model.qry.MemberQuery;
import com.whoiszxl.query.model.response.MemberResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理后台相关接口
 *
 * @author whoiszxl
 * @date 2022/1/24
 */
@RestController
@RequestMapping("/member")
@Tag(name = "用户管理后台 API")
@RequiredArgsConstructor
public class MemberAdminController {

    private final MemberApplicationService memberApplicationService;

    private final MemberQueryApplicationService memberQueryApplicationService;

    @SaCheckLogin
    @PostMapping("/page/list")
    @Operation(summary = "分页获取用户列表", description = "分页获取用户列表")
    protected ResponseResult<PageResponse<MemberResponse>> page(@RequestBody @Validated MemberQuery query) {
        PageResponse<MemberResponse> list = this.memberQueryApplicationService.list(query);
        return ResponseResult.buildSuccess(list);

    }

    @Operation(summary = "切换用户的状态", description = "切换用户的状态")
    @PatchMapping("/switch/status/{memberId}")
    @SaCheckPermission(value = {"member:member:switch-status"})
    public ResponseResult<String> switchStatus(@PathVariable Integer memberId) {
        Boolean flag = memberApplicationService.switchStatus(memberId);
        return ResponseResult.buildByFlag(flag);
    }

}
